<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-row style="margin-bottom:15px">
        <el-col>
          <el-button
            type="primary"
            @click="showAddCateDialog"
          >添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <tree-table
        :data='catelist'
        :columns="columns"
        :expand-type='false'
        :selection-type='false'
        show-index
        border
        index-text='#'
      >
        <!-- v-slot 等同于 slot = 'isOk' slog-scope = 'scope' 是否有效 -->
        <template v-slot:isok='scope'>
          <!-- {{scope.row.cat_deleted}} -->
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false "
            style="color:lightgreen;"
          ></i>
          <i
            class="el-icon-error"
            v-else
            style="color:lightred;"
          ></i>
        </template>
        <!-- 排序 -->
        <template v-slot:order='scope'>
          <!-- {{scope.row.cat_deleted}} -->
          <el-tag
            size="mini"
            v-if="scope.row.cat_level === 0"
          >一级</el-tag>
          <el-tag
            type="success"
            size="mini"
            v-else-if="scope.row.cat_level === 1"
          >二级</el-tag>
          <el-tag
            type="warning"
            size="mini"
            v-else
          >三级</el-tag>
        </template>
        <!-- 操作 -->
        <!-- eslint-disable-next-line -->
        <template v-slot:opt='scope'>
          <!-- 修改按钮 -->
          <!-- {{scope.row.cat_deleted}} -->
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="showEditDialog(scope.row.cat_id)"
          >编辑</el-button>
          <!-- 删除按钮 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="deleteCate(scope.row.cat_id)"
          >删除</el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addDialogVisible"
      width="60%"
      @close="addDialogClosed"
    >
      <span>在下面表格中填写添加信息</span>
      <!-- 表单填写 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="100px"
      >
        <el-form-item
          label="分类名称:"
          prop="cat_name"
        >
          <el-input v-model="addForm.cat_name"></el-input>
        </el-form-item>
        <div class="block">
          <el-form-item label="父级分类:">
            <el-cascader
              clearable
              v-model="selectKeys"
              :options="cateParentList"
              :props="cascaderProps"
              @change="parentCateChange"
            ></el-cascader>
          </el-form-item>
        </div>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addCate"
        >确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑分类 -->
    <el-dialog
      title="编辑分类"
      :visible.sync="editDialogVisible"
      width="60%"
    >
      <el-form
        :model="editForm"
        label-width="100px"
        :rules="editFormRules"
        ref="editFormRef"
      >
        <el-form-item
          label="分类名称"
          prop="cat_name"
        >
          <el-input v-model="editForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="saveEditCate"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
export default {
  data () {
    return {
      queryInfo: { // 查询参数
        type: 3,
        pagenum: 1,
        pagesize: 10
      },
      catelist: [], // 商品数据
      total: 0, // 总数据条数
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
          width: '400px',
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      // 添加分类表单
      addForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0,

      },
      editForm: {
        cat_name: ''
      },
      // 添加分类规则
      addFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名', trigger: 'blur' }
        ]
      },
      editFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名', trigger: 'blur' }
        ]
      },
      addDialogVisible: false,
      editDialogVisible: false,
      // 编辑对象id
      editCateId: '',
      // 父级分类列表
      cateParentList: [],
      // 配置选项
      cascaderProps: {
        expandTrigger: 'hover',
        checkStrictly: true,
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      selectKeys: []
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    // 获取分类列表
    async getCateList () {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取用户列表失败！')
      }
      this.catelist = res.data.result
      this.total = res.data.total
      //   console.log('1,', res, this.catelist, this.total)
    },
    // pagesize改变监听 每页数据条数
    handleSizeChange (newPagesize) {
      this.queryInfo.pagesize = newPagesize
      this.getCateList()
    },
    // 当前页码改变
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getCateList()
    },
    // 分类添加输入框
    showAddCateDialog () {
      this.addDialogVisible = true
      this.getCateParentList()
    },
    // 分类添加框关闭
    addDialogClosed () {
      this.$refs.addFormRef.resetFields()
      this.selectKeys = []
      this.addForm.cat_pid = 0
      this.addForm.cat_level = 0
    },
    async getCateParentList () {
      const { data: res } = await this.$http.get('categories', { params: { type: [2] } })
      if (res.meta.status !== 200) {
        this.$message.error('获取分类列表失败！')
      }
      //   console.log(res.data)
      this.cateParentList = res.data
    },
    parentCateChange () {
      //   console.log(this.selectKeys)
      if (this.selectKeys.length >= 1) {
        this.addForm.cat_pid = this.selectKeys[this.selectKeys.length - 1]
        // 分类分级
        this.addForm.cat_level = this.selectKeys.length
      } else {
        this.addForm.cat_pid = 0
        this.addForm.cat_level = 0
      }
      //   console.log(this.addForm)
    },
    // 保存分类信息
    addCate () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('请输入添加的分类名')
        }
        const { data: res } = await this.$http.post('categories', this.addForm)
        if (res.meta.status !== 201) {
          //   console.log(res)
          return this.$message.error('添加分类失败')
        }
        this.$message.success('添加分类成功')
        // console.log(res.data)
      })
      this.getCateList()
      this.addDialogVisible = false
      //   console.log(this.addForm)
    },
    // 编辑分类框
    async showEditDialog (cateid) {
      this.editCateId = cateid
      const { data: res } = await this.$http.get('categories/' + cateid)
      if (res.meta.status !== 200) {
        this.$message.error('获取数据失败')
      }
      this.editForm.cat_name = res.data.cat_name
      this.editDialogVisible = true
      //   console.log(cateid)
    },
    // 提交编辑内容
    saveEditCate () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('请输入添加的分类名')
        }
        const { data: res } = await this.$http.put('categories/' + this.editCateId, this.editForm)
        if (res.meta.status !== 200) {
          //   console.log(res)/
          return this.$message.error('添加分类失败')
        }
        this.$message.success('添加分类成功')
        // console.log(res.data)/
      })
      this.editDialogVisible = false
      this.getCateList()
    },
    async deleteCate (cateid) {
      const res = await this.$confirm('此操作将永久删除该商品分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // console.log(res)
      if (res !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res1 } = await this.$http.delete('categories/' + cateid)
      if (res1.meta.status !== 200) {
        return this.$message.error('删除用户失败！')
      }
      this.$message.success('删除用户成功！')
      this.getCateList()
    }
  }
}
</script>

<style lang="less">
.el-cascader-panel {
  height: 200px;

  //   overflow: hidden;
}
.el-cascader {
  width: 100%;
}
</style>
